package cn.iocoder.yudao.module.acm.service.tablewareproduceplan;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.acm.controller.admin.tablewareproduceplan.vo.TablewarePlanDetailRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.tablewareproduceplan.vo.TablewareProducePlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.tablewareproduceplan.vo.TablewareProducePlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.tablewareproduceplan.vo.TablewareProducePlanSaveReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.tablewareplandetail.TablewarePlanDetailDO;
import cn.iocoder.yudao.module.acm.dal.dataobject.tablewareproduceplan.TablewareProducePlanDO;
import cn.iocoder.yudao.module.acm.dal.mysql.tablewareplandetail.TablewarePlanDetailMapper;
import cn.iocoder.yudao.module.acm.dal.mysql.tablewareproduceplan.TablewareProducePlanMapper;
import cn.iocoder.yudao.module.acm.enums.ACMCabinClassEnum;
import cn.iocoder.yudao.module.pplan.enums.PPlanProduceMarkStatusEnum;
import cn.iocoder.yudao.module.pplan.enums.PPlanProduceStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.acm.enums.ErrorCodeConstants.TABLEWARE_PRODUCE_PLAN_NOT_EXISTS;

/**
 * 航机管理-餐具生产计划 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class TablewareProducePlanServiceImpl implements TablewareProducePlanService {

    @Resource
    private TablewareProducePlanMapper tablewareProducePlanMapper;
    @Resource
    private TablewarePlanDetailMapper tablewarePlanDetailMapper;

    @Override
    public Long createTablewareProducePlan(TablewareProducePlanSaveReqVO createReqVO) {
        // 插入
        TablewareProducePlanDO tablewareProducePlan = BeanUtils.toBean(createReqVO, TablewareProducePlanDO.class);
        tablewareProducePlan.setMarkStatus(Integer.valueOf(PPlanProduceMarkStatusEnum.MARK_STATUS_1.getCode()));
        tablewareProducePlan.setStatus(Integer.valueOf(PPlanProduceStatusEnum.STATUS_ENUM_1.getCode()));
        tablewareProducePlan.setPlanCode("T"+System.currentTimeMillis());
        tablewareProducePlanMapper.insert(tablewareProducePlan);
        //插入餐具详细
        List<TablewarePlanDetailDO> detailDOS = new ArrayList<>();
        if (CollUtil.isNotEmpty(createReqVO.getFrontDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getFrontDetailList(), TablewarePlanDetailDO.class, clueVO -> {
                clueVO.setTablewarePlanId(tablewareProducePlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(createReqVO.getMiddleDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getMiddleDetailList(), TablewarePlanDetailDO.class, clueVO -> {
                clueVO.setTablewarePlanId(tablewareProducePlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(createReqVO.getRearDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getRearDetailList(), TablewarePlanDetailDO.class, clueVO -> {
                clueVO.setTablewarePlanId(tablewareProducePlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
            }));
        }
        tablewarePlanDetailMapper.insertBatch(detailDOS);
        // 返回
        return tablewareProducePlan.getId();
    }

    @Override
    public void updateTablewareProducePlan(TablewareProducePlanSaveReqVO updateReqVO) {
        // 校验存在
        validateTablewareProducePlanExists(updateReqVO.getId());
        // 更新
        TablewareProducePlanDO updateObj = BeanUtils.toBean(updateReqVO, TablewareProducePlanDO.class);
        tablewareProducePlanMapper.updateById(updateObj);

        // 机供品详情更新-先删除-再新增
        tablewarePlanDetailMapper.delete(TablewarePlanDetailDO::getTablewarePlanId, updateReqVO.getId());
        //插入机供品详细
        List<TablewarePlanDetailDO> detailDOS = new ArrayList<>();
        if (CollUtil.isNotEmpty(updateReqVO.getFrontDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getFrontDetailList(), TablewarePlanDetailDO.class, clueVO -> {
                clueVO.setTablewarePlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(updateReqVO.getMiddleDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getMiddleDetailList(), TablewarePlanDetailDO.class, clueVO -> {
                clueVO.setTablewarePlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(updateReqVO.getRearDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getRearDetailList(), TablewarePlanDetailDO.class, clueVO -> {
                clueVO.setTablewarePlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
            }));
        }
        tablewarePlanDetailMapper.insertBatch(detailDOS);
    }

    @Override
    public void deleteTablewareProducePlan(Long id) {
        // 校验存在
        validateTablewareProducePlanExists(id);
        // 删除
        tablewareProducePlanMapper.deleteById(id);
        tablewarePlanDetailMapper.delete(TablewarePlanDetailDO::getTablewarePlanId, id);
    }

    @Override
        public void deleteTablewareProducePlanListByIds(List<Long> ids) {
        // 校验存在
        validateTablewareProducePlanExists(ids);
        // 删除
        tablewareProducePlanMapper.deleteByIds(ids);
        }

    private void validateTablewareProducePlanExists(List<Long> ids) {
        List<TablewareProducePlanDO> list = tablewareProducePlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TABLEWARE_PRODUCE_PLAN_NOT_EXISTS);
        }
    }

    private void validateTablewareProducePlanExists(Long id) {
        if (tablewareProducePlanMapper.selectById(id) == null) {
            throw exception(TABLEWARE_PRODUCE_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public TablewareProducePlanRespVO getTablewareProducePlan(Long id) {

        TablewareProducePlanDO tablewareProducePlanDO = tablewareProducePlanMapper.selectById(id);
        TablewareProducePlanRespVO tablewareProducePlanRespVO = BeanUtils.toBean(tablewareProducePlanDO, TablewareProducePlanRespVO.class);

        List<TablewarePlanDetailDO> detailDOS = tablewarePlanDetailMapper.selectList(TablewarePlanDetailDO::getTablewarePlanId, id);
        if (CollUtil.isNotEmpty(detailDOS)) {
            List<TablewarePlanDetailRespVO> respVOS = BeanUtils.toBean(detailDOS, TablewarePlanDetailRespVO.class);
            Map<String, List<TablewarePlanDetailRespVO>> collect = respVOS.stream().collect(Collectors.groupingBy(TablewarePlanDetailRespVO::getCabainClass));
            tablewareProducePlanRespVO.setFrontDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            tablewareProducePlanRespVO.setMiddleDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            tablewareProducePlanRespVO.setRearDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
        }
        return tablewareProducePlanRespVO;
    }

    @Override
    public PageResult<TablewareProducePlanDO> getTablewareProducePlanPage(TablewareProducePlanPageReqVO pageReqVO) {
        return tablewareProducePlanMapper.selectPage(pageReqVO);
    }

}